/**
 * There are <a href="https://github.com/ketayao/keta-custom">keta-custom</a> code generation
 */
package fms.dao;

import java.util.List;

import fms.model.CptSend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service("cptSendService")
public class CptSendServiceImpl implements CptSendService {

	@Autowired
	private CptSendDAO cptSendDAO;

	/*
	 * (non-Javadoc)
	 * @see com.sample.service.TaskService#get(java.lang.Long)
	 */
	@Override
	public CptSend get(Long id) {
		return cptSendDAO.findOne(id);
	}

	/*
	 * (non-Javadoc)
	 * @see com.sample.service.TaskService#saveOrUpdate(com.sample.entity.Task)
	 */
	@Override
	public void saveOrUpdate(CptSend cptSend) {
		List<CptSend> cptSendList = cptSendDAO.findCptSend2(cptSend.getFlightId(), cptSend.getAircraftId(), cptSend.getPlanId(),cptSend.getSendType());
		if(cptSendList != null && cptSendList.size() > 0){
			for(CptSend send : cptSendList){
				if(cptSend.getSendType().equals("2")) {
					cptSendDAO.delete(send.getId());
				}
			}
		}
		cptSendDAO.save(cptSend);
	}

	/*
	 * (non-Javadoc)
	 * @see com.sample.service.TaskService#delete(java.lang.Long)
	 */
	@Override
	public void delete(Long id) {
		cptSendDAO.delete(id);
	}




	// 飞行计划上传所需的字符串
	public String buildMsg(String flightId, String aircraftId, String flyPlanValidTime) {
		List list = findCptSend(flightId, aircraftId, flyPlanValidTime);
		CptSend cptSend = (CptSend) list.get(0);
		return FLPMsgBuilder.build(cptSend);
	}

	// 按航班号、机位号查询比flyPlanValidTime日期(UTC时间)大的飞行机计划
	public List<CptSend> findCptSend(String flightId, String aircraftId, String flyPlanValidTime) {
		return cptSendDAO.findCptSend(flightId, aircraftId, flyPlanValidTime);
	}


	public List<CptSend> findCptSendwid(String flightId,String aircraftId) {
		return cptSendDAO.findCptSendwid(flightId,aircraftId);

	}

	public List<CptSend> findCptSendbymsgId(String appUpMsgId){
		return cptSendDAO.findCptSendbymsgId(appUpMsgId);
	}
}
